package com.zj.leetcode.medium.hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: algorithm
 * @description: 在系统中查找重复文件
 * 609 find-duplicate-file-in-system
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class FindDuplicateFileInSystem {
    public static void main(String[] args) {

        String[] paths = {
                "root/a 1.txt(abcd) 2.txt(efgh)",
                "root/c 3.txt(abcd)",
                "root/c/d 4.txt(efgh)",
                "root 4.txt(efgh)"};

        Solution solution = new FindDuplicateFileInSystem().new Solution();
        System.out.println(solution.findDuplicate(paths));

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<List<String>> findDuplicate(String[] paths) {

            //key 内容， value 多个地址
            HashMap<String, List<String>> map = new HashMap<>();
            for (int i = 0; i < paths.length; i++) {
                String[] strArr = paths[i].split(" ");
                String path = "";
                if (strArr.length > 1) {
                    path = strArr[0];
                }
                for (int j = 1; j < strArr.length; j++) {
                    String[] split = strArr[j].split("[(]");
                    String content = split[1];
                    List<String> list = map.getOrDefault(content, new ArrayList<>());
                    list.add(path + "/" + split[0]);
                    map.put(content, list);
                }
            }

            List<List<String>> ret =new ArrayList<>();
            for (List<String> list: map.values()){
                if(list.size() > 1){
                    ret.add(list);
                }
            }
            return ret;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
